package cn.lmxdev.ptms.service.impl;

import cn.lmxdev.ptms.bean.exception.BizException;
import cn.lmxdev.ptms.bean.table.TSpecialty;
import cn.lmxdev.ptms.dao.ISpecialtyDao;
import cn.lmxdev.ptms.service.ISpecialtyServlet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@Service
public class SpecialtyServletImpl implements ISpecialtyServlet {
    private final ISpecialtyDao specialtyDao;

    @Autowired
    public SpecialtyServletImpl(
            ISpecialtyDao specialtyDao
    ) {
        this.specialtyDao = specialtyDao;
    }

    @Override
    @Transactional
    public TSpecialty get(Long id) {
        return this.specialtyDao.find(id);
    }

    @Override
    @Transactional
    public long add(TSpecialty specialty) {
        // 判断名称是否已经存在
        TSpecialty specialtyByName = this.specialtyDao.findByName(specialty.getName());
        if (specialtyByName != null && specialtyByName.getId() > 0)
            throw new BizException("操作失败，该名称已经存在");

        // 插入数据
        if (!this.specialtyDao.insert(specialty))
            throw new BizException("操作失败");

        // 返回ID
        return specialty.getId();
    }

    @Override
    @Transactional
    public void remove(Long id) {
        // 删除数据
        if (!this.specialtyDao.delete(id))
            throw new BizException("操作失败");
    }

    @Override
    @Transactional
    public void edit(TSpecialty specialty) {
        // 查询旧数据
        TSpecialty oldSpecialty = this.specialtyDao.find(specialty.getId());

        // 判断名称是否已经存在
        TSpecialty specialtyByName = this.specialtyDao.findByName(specialty.getName());
        if (specialtyByName != null && !Objects.equals(specialtyByName.getId(), oldSpecialty.getId()))
            throw new BizException("操作失败，该名称已经存在");

        // 更新属性
        oldSpecialty.setSpecialtyHeadId(specialty.getSpecialtyHeadId())
                .setName(specialty.getName());

        // 保存数据
        if (!this.specialtyDao.update(oldSpecialty))
            throw new BizException("操作失败");
    }

    @Override
    @Transactional
    public long count(TSpecialty specialty) {
        return this.specialtyDao.count(specialty);
    }

    @Override
    @Transactional
    public List<TSpecialty> list(Long limit, Long page, TSpecialty specialty) {
        return this.specialtyDao.select(page != null ? (page - 1) * limit : null, limit, specialty);
    }

    @Override
    @Transactional
    public long departmentHeadCount(Long departmentHeadId, TSpecialty specialty) {
        return this.specialtyDao.departmentHeadCount(departmentHeadId, specialty);
    }

    @Override
    @Transactional
    public List<TSpecialty> departmentHeadList(Long departmentHeadId, Long limit, Long page, TSpecialty specialty) {
        return this.specialtyDao.departmentHeadSelect(departmentHeadId, page != null ? (page - 1) * limit : null, limit, specialty);
    }

    @Override
    @Transactional
    public long adviserCount(Long adviserId, TSpecialty specialty) {
        return this.specialtyDao.adviserCount(adviserId, specialty);
    }

    @Override
    @Transactional
    public List<TSpecialty> adviserList(Long adviserId, Long limit, Long page, TSpecialty specialty) {
        return this.specialtyDao.adviserSelect(adviserId, page != null ? (page - 1) * limit : null, limit, specialty);
    }
}
